package com.meixiang.activity.account.myfund;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.activity.WebviewActivity;
import com.meixiang.adapter.myFundAdapter.FundOptionalSortAdapter;
import com.meixiang.fragment.fund.HoldFragment;
import com.meixiang.fragment.fund.OptionalFragment;
import com.meixiang.fragment.fund.OptionalSortFragment;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.view.TitleView;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 我的基金
 * Created by xyj on 2016/5/30.
 */
public class MyFundActivity extends BaseActivity {

    @Bind(R.id.app_title)
    TitleView titleView; //头部视图
    @Bind(R.id.tv_fund_hold)
    TextView tv_fund_hold; //持有基金
    @Bind(R.id.tv_fund_optional)
    TextView tv_fund_optional; //自选基金
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    public static final String TAG = "MyFundActivity";
    private boolean isSave = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_fund);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        titleView.setBackgroundColor(ContextCompat.getColor(context, R.color.fund_money_color));
        titleView.setTitle("我的基金");
        titleView.setLeftImageButton(R.mipmap.month_3);
        titleView.setRightImageButton(R.mipmap.doubt);
        titleView.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleView.showRightButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tool.showTextToast(context, "详情");
            }
        });
        titleView.setTColor(ContextCompat.getColor(context, R.color.white));
        tv_fund_hold.setOnClickListener(this);
        tv_fund_optional.setOnClickListener(this);
    }

    @Override
    protected void setData() {
        changSwitch(1);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.tv_fund_hold: //持有资金
                changSwitch(1);
                break;

            case R.id.tv_fund_optional: //自选基金
                changSwitch(2);
                break;
        }
    }

    /**
     * 切换数据
     *
     * @param id
     */
    private void changSwitch(int id) {
        fragmentManager = getSupportFragmentManager();
        Bundle bundle = new Bundle();
        transaction = fragmentManager.beginTransaction();
        switch (id) {
            case 1:
                tv_fund_hold.setBackgroundResource(R.drawable.selector_while_bg_lift_red_border_text);
                tv_fund_hold.setTextColor(ContextCompat.getColor(context, R.color.white));
                tv_fund_optional.setBackgroundResource(R.drawable.selector_while_bg_right_border_text);
                tv_fund_optional.setTextColor(ContextCompat.getColor(context, R.color.fund_money_color));
                titleView.setRightImageButton(R.mipmap.doubt);
                titleView.setRightTextButton(null);
                FundOptionalSortAdapter.clearFundIdStr();
                titleView.showRightButton(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, WebviewActivity.class);
                        intent.putExtra("title", "说明");
                        intent.putExtra("url", Config.FUND_NOTICE);
                        startActivity(intent);
                    }
                });
                isSave = false;
                transaction.replace(R.id.content_frame, new HoldFragment());
                break;

            case 2:
                tv_fund_hold.setBackgroundResource(R.drawable.selector_while_bg_lift_border_text);
                tv_fund_hold.setTextColor(ContextCompat.getColor(context, R.color.fund_money_color));
                tv_fund_optional.setBackgroundResource(R.drawable.selector_while_bg_right_red_border_text);
                tv_fund_optional.setTextColor(ContextCompat.getColor(context, R.color.white));
                titleView.setRightTextButton("编辑");
                titleView.setRightImageButton(0);
                FundOptionalSortAdapter.clearFundIdStr();
                titleView.setRightTextCorlr(ContextCompat.getColor(context, R.color.white));
                if (isSave) {
                    bundle.putParcelableArrayList("optionList", OptionalSortFragment.getOptionList());
                }
                transaction.replace(R.id.content_frame, new OptionalFragment().getIntence(bundle));
                titleView.showRightButton(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        changSwitch(3);
                    }
                });
                break;
            case 3:
                titleView.setRightTextButton("保存");
                titleView.setRightImageButton(0);
                bundle.putParcelableArrayList("optionList", OptionalFragment.getOptionList());
                titleView.setRightTextCorlr(ContextCompat.getColor(context, R.color.white));
                transaction.replace(R.id.content_frame, new OptionalSortFragment().getIntance(bundle));
                titleView.showRightButton(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        isSave = true;
                        if (FundOptionalSortAdapter.isDelete) { //是否是移除自选
                            SaveOptionalData();
                        }
                        changSwitch(2);
                    }
                });
                break;
        }
        transaction.commit();
    }

    /**
     * 保存数据
     */
    private void SaveOptionalData() {
        String fund_id_str = "";
        for (int i = 0; i < FundOptionalSortAdapter.getFundIdStr().size(); i++) {
            if (i == FundOptionalSortAdapter.getFundIdStr().size() - 1) {
                fund_id_str += FundOptionalSortAdapter.getFundIdStr().get(i);
            } else {
                fund_id_str += FundOptionalSortAdapter.getFundIdStr().get(i) + ",";
            }
        }
        String removeUrl = Config.FUND_MOVE_SELECT + "?fund_id_str=" + fund_id_str;
        HttpUtils.getC(removeUrl, new HttpCallBack(MyFundActivity.this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                Tool.showTextToast(context, message);
            }

            @Override
            public void onError(String code, String message) {
                Tool.showTextToast(context, message);
            }

            @Override
            public void onResponse(boolean isFromCache, JSONObject jsonObject, Request request, Response response) {
                super.onResponse(isFromCache, jsonObject, request, response);
                if (jsonObject.optInt("code") == 0) {
                    Tool.showTextToast(context, "移除自选成功");
                } else {
                    Tool.showTextToast(context, "移除自选失败");
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        HttpUtils.tagCancelRequest(TAG);
    }
}
